package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.access.EntityMjRegionInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoRegionInfo;
import com.ocom.srvpaccess.service.RegionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RegionInfoServiceImpl extends ServiceImpl<IDaoRegionInfo, EntityMjRegionInfo> implements RegionInfoService {


    @Autowired
    private IDaoRegionInfo daoRegionInfo;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjRegionInfo GetRegionInfo(Long regionId, Long comId) {
        QueryWrapper<EntityMjRegionInfo> wrapper = Wrappers.query();
        wrapper.eq("com_id",comId);
        if (StringUtils.isNotNull(regionId)) {
            wrapper.eq("region_id", regionId);
        }
        wrapper.last("limit 1");
        return daoRegionInfo.selectOne(wrapper);

    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjRegionInfo> selectGetRegionInfo(Long comId, Integer regionId, Integer deptId, String regionName) {
        QueryWrapper<EntityMjRegionInfo> wrapper = Wrappers.query();
        wrapper.eq("com_id",comId);
        if (regionId != null){
            wrapper.eq("region_id",regionId);
        }
        if (deptId != null){
            wrapper.eq("dept_id",deptId);
        }
        if (regionName!=null){
            wrapper.like("region_name",regionName);
        }
        return daoRegionInfo.selectList(wrapper);
    }
}
